package prueba;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class RecordStorem extends MIDlet implements CommandListener {

	private RecordStore rs = null;
	static final String REC_STORE = "dbprueba";
	private Display pantalla;
	private Form form,crear;
	private Command comandocrear,confirmar;
	TextField creacampo;
	
	public RecordStorem() throws RecordStoreException {
		// TODO Auto-generated constructor stub
		pantalla = Display.getDisplay(this);
		rs = RecordStore.openRecordStore(REC_STORE, true );
		
		readRecords();
		
		form=new Form("RecordStore Prueba");
		crear=new Form("Crear registro");
		creacampo= new TextField("Nuevo Campo:","",10,TextField.ANY);
		crear.append(creacampo);
		comandocrear=new Command("Crear Campo",Command.OK, 0);
		confirmar=new Command("Confirmar",Command.OK, 0);
		form.addCommand(comandocrear);
		
		crear.addCommand(confirmar);
		pantalla.setCurrent(form);
		form.setCommandListener(this);
		crear.setCommandListener(this);
		
		
		 
		 
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		try {
			rs.closeRecordStore();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	public void commandAction(Command arg0, Displayable arg1) {
		// TODO Auto-generated method stub
		if(arg0.equals(comandocrear)){
			pantalla.setCurrent(crear);
		}else if(arg0.equals(confirmar)){
			writeRecord(creacampo.getString());
			creacampo.delete(0, creacampo.size());
			pantalla.setCurrent(form);
			form.deleteAll();
			readRecords();
			
		}
	}
	
	public void writeRecord(String str)
	  {
	    byte[] rec = str.getBytes();

	    try
	    {
	      rs.addRecord(rec, 0, rec.length);
	    }
	    catch (Exception e)
	    {
	      
	    }
	  }
	
	 public void readRecords()
	  {
	    try
	    {
	      // Intentionally make this too small to test code below
	      byte[] recData = new byte[5]; 
	      int len;

	      for (int i = 1; i <= rs.getNumRecords(); i++)      
	      {
	        if (rs.getRecordSize(i) > recData.length)
	          recData = new byte[rs.getRecordSize(i)];
	       
	        len = rs.getRecord(i, recData, 0);
	        form.append("Record #" + i + ": " + new String(recData, 0, len) + "\n");                            
	      }
	    }
	    catch (Exception e)
	    {
	      
	    }
	  }

}
